package com.qf.smartplatform.service.impl;

import com.qf.smartplatform.cache.RoleCache;
import com.qf.smartplatform.events.RoleChangeEvent;
import com.qf.smartplatform.exceptions.AddDataException;
import com.qf.smartplatform.exceptions.DeleteDataException;
import com.qf.smartplatform.exceptions.QueryDataException;
import com.qf.smartplatform.exceptions.ResultCode;
import com.qf.smartplatform.mapper.RoleMapper;
import com.qf.smartplatform.pojo.CheckType;
import com.qf.smartplatform.pojo.Role;
import com.qf.smartplatform.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * projectName:day12_27_Test
 * author:guoLei
 * time:2022/1/10 11:12
 * description:
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    private RoleMapper roleMapper;
    private ApplicationContext applicationContext;
    private RoleCache roleCache;
@Autowired
    public void setRoleCache(RoleCache roleCache) {
        this.roleCache = roleCache;
    }

    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Autowired
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }


    @Override
    public void addRole(Role role) {
        Assert.isTrue(!role.isEmpty(CheckType.ADD), () -> {
            throw new AddDataException("必须传输的数据不完整", ResultCode.ROLENAME_NULL);
        });
        Role role1 = roleMapper.selectRoleById(role.getRoleId());
        Assert.isNull(role1, () -> {
            throw new AddDataException("角色名称已存在", ResultCode.DATA_ALREADY_EXIST);
        });
        String roleName = role.getRoleName();
        String createBy = role.getCreateBy();
        String remark = role.getRemark();
        role.setCreateTime(new Date());
        role.setCreateBy(role.getRoleName());
        applicationContext.publishEvent(new RoleChangeEvent());
    }

    @Override
    public int updateRole(Role role) {
return 0;
    }

    @Override
    public int deleteRoleByIds(List<Long> roleId) {
        Assert.notEmpty(roleId,()->{
            throw new DeleteDataException("没有传递角色id",ResultCode.ID_NULL);
        });
        int i = roleMapper.deleteRoleById(roleId);
        applicationContext.publishEvent(new RoleChangeEvent());
        return i;
    }

    @Override
    public List<Role> selectAllRole() {
        List<Role> allData = roleCache.getAllData();
        return allData;
    }

    @Override
    public List<Role> selectRolesByNameLike(String roleName) {
        Assert.hasText(roleName,()->{
            throw new QueryDataException("分类名为空",ResultCode.QUERY_PARAM_EMPTY);
        });
        List<Role>roleList=roleCache.getAllData();
        List<Role>roleList1=roleList.stream().filter(role -> role.getRoleName().contains(roleName))
                .collect(Collectors.toList());
        return roleList1;
    }

    @Override
    public Role selectById(Long roleId) {
        Assert.isTrue(roleId > 0, () -> {
            throw new QueryDataException("主键超出允许的范围", ResultCode.ID_NOT_ALLOWED);
        });
        Role role = roleCache.getValue(roleId);
        return role;
    }


}
